# ctrl + f "!NOTE" for notes
import urllib2

import es
import cfglib
import services
import services.auth

from libs.encryption import Encrypter

info = es.AddonInfo()
info.basename = "webauth"
info.name     = "Web Authorization"
info.version  = "1.0.000"
info.author   = "freddukes (Steven Hartin)"

encryption = Encrypter()

class Connection(object):
    def __init__(self, webDomain, username, password):
        self.domain = webDomain
        self.isOpen = False
        self.openObject = None
        self.username = username
        self.password = password
        
    def __del__(self):
        self.disconnect()
        
    def disconnect(self):
        if self.isOpen:
    
    def open(self):
        self.isOpen     = True
        self.openObject = urllib2.urlopen(self.webDomain, "username=%s&password=%s"%(self.username, encryption.md5(self.password) ) )
        
class AuthorizationManager(object):
    def __init__(self, webDomain, username, password):
        self.connection = Connection(webDomain, username, password)
        
    def __del__(self):
        pass
        
    def update(self):
        pass

class WebAuthorization(services.auth.AuthorizationService):
    """ Child class of an authorization service """
    def __init__(self):
        self.authManager = AuthorizationManager(str(domain), <USERNAME>, <PASSWORD>)
        
    def __del__(self):
        pass
        
    def registerCapability(self, auth_capability, auth_recommendedlevel):
        pass

    def isUseridAuthorized(self, auth_userid, auth_capability):
        pass

    def getOfflineIdentifier(self, auth_userid):
        pass

    def isIdAuthorized(self, auth_identifier, auth_capability):
        pass
    
config   = cfglib.AddonCFG( os.path.join( es.getAddonPath(info.basename), "config.cfg" ) )
domain   = config.cvar("webauth_domain", "www.mydomain.com", "The URL of the site where the web folder is uploaded to, NOTE: Do not include the http://")
username = config.cvar("webauth_username", "username", "The username that you used when you installed the website")
password = config.cvar("webauth_password", "password", "The password that you used when you installed the website")
config.write()
config.execute()

webAuth = WebAuthorization()

def load():
    services.register("auth", webAuth)
    
def unload():
    services.unregiser("auth")